package com.lineage.server.command.executor;

import com.lineage.config.ConfigRate;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_GmMessage;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.world.World;
import java.util.StringTokenizer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1DbDropMoney implements L1CommandExecutor {
  private static final Log _log = LogFactory.getLog(L1DbDropMoney.class);
  
  public static L1CommandExecutor getInstance() {
    return new L1DbDropMoney();
  }
  
  public void execute(L1PcInstance pc, String cmdName, String arg) {
    try {
      StringTokenizer st = new StringTokenizer(arg);
      int rateDrop = Integer.parseInt(st.nextToken(), 10);
      String msgid = null;
      if (pc == null)
        _log.warn("系统命令执行: " + cmdName + " 变更掉钱率" + arg); 
      if ((int)ConfigRate.RATE_DROP_ADENA == rateDrop) {
        if (pc == null) {
          _log.warn("目前掉钱率已经是: " + rateDrop);
        } else {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, "目前掉钱率已经是:" + rateDrop));
        } 
        return;
      } 
      if (ConfigRate.RATE_DROP_ADENA < rateDrop) {
        ConfigRate.RATE_DROP_ADENA = rateDrop;
        msgid = "金币倍率变更为" + ConfigRate.RATE_DROP_ADENA + "倍。";
      } else if (ConfigRate.RATE_DROP_ADENA > rateDrop) {
        ConfigRate.RATE_DROP_ADENA = rateDrop;
        msgid = "金币倍率恢复为" + ConfigRate.RATE_DROP_ADENA + "倍。";
      } 
      if (msgid != null)
        World.get().broadcastPacketToAll((ServerBasePacket)new S_GmMessage(msgid, "\\aE")); 
      if (pc == null)
        _log.warn("目前掉钱率变更为: " + rateDrop); 
    } catch (Exception e) {
      if (pc == null) {
        _log.error("错误的命令格式: " + getClass().getSimpleName());
      } else {
        _log.error("错误的GM指令格式: " + getClass().getSimpleName() + " 执行的GM:" + pc.getName());
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(261));
      } 
    } 
  }
}
